package com.java.s6;

public class singleton {
    //volatile 保证 不可以重排序
    private static volatile singleton INSTANCE; //JIT

    private singleton(){};

    public static singleton getInstance(){
        //业务代码
        if (INSTANCE == null){  //d c l
            //双重检查
            synchronized (singleton.class){
                if (INSTANCE == null) {
                    try{
                        Thread.sleep(1);
                    }
                    catch (InterruptedException e){
                        e.printStackTrace();
                    }
                    INSTANCE = new singleton();
                }
            }
        }
        return INSTANCE;
    }

    public void m(){
        System.out.println("m");
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(()->{
                System.out.println(singleton.getInstance().hashCode());
            }).start();
        }
    }
}
